package com.cuzofu.fs.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cuzofu.fs.common.PageUtils;
import com.cuzofu.fs.common.R;
import com.cuzofu.fs.entity.BlueprintEntity;
import com.cuzofu.fs.entity.UnitEntity;
import com.cuzofu.fs.service.BlueprintService;
import com.cuzofu.fs.vo.BlueprintHistoryVO;
import com.cuzofu.fs.vo.BlueprintVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * 图纸信息
 *
 * @author cuzofu
 * @email cuzofu@qq.com
 * @date 2020-07-10 08:44:48
 */
@RestController
@RequestMapping("/blueprint")
public class BlueprintController {
    @Autowired
    private BlueprintService blueprintService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = blueprintService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") String id) {
        BlueprintEntity blueprint = blueprintService.getById(id);

        return R.ok().put("blueprint", blueprint);
    }

    /**
     * 审查专业图纸列表
     */
    @RequestMapping("/infoList/{mid}")
    public R infoList(@PathVariable("mid") String mid) {
        QueryWrapper<BlueprintVO> qw = new QueryWrapper<>();
        qw.eq("mid", mid);
        qw.orderByDesc("create_time");
        List<BlueprintVO> list = blueprintService.queryBpList(qw);
        return R.ok().put("list", list);
    }

    /**
     * 审查专业图纸列表
     */
    @RequestMapping("/historyInfoList/{mid}")
    public R historyInfoList(@PathVariable("mid") String mid) {
        List<BlueprintHistoryVO> list = blueprintService.queryBlueprintHistoryByMid(mid);
        return R.ok().put("list", list);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody BlueprintEntity blueprint) {
        blueprintService.save(blueprint);

        return R.ok().put("data", blueprint);
    }

    /**
     * 创建图纸（初审上传）
     */
    @RequestMapping("/create")
    public R create(@RequestBody BlueprintVO blueprint) {
        BlueprintEntity rtn = blueprintService.create(blueprint);

        QueryWrapper<BlueprintVO> qw = new QueryWrapper<>();
        qw.eq("mid", rtn.getMid());
        qw.orderByDesc("create_time");
        List<BlueprintVO> list = blueprintService.queryBpList(qw);

        return R.ok().put("data", list);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody BlueprintEntity blueprint) {
        blueprintService.updateById(blueprint);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] ids) {
        blueprintService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
